﻿using MyProduce.enumeration;
using MyProduce.MyProduce.ProduceDto;
using MyProduce.Produce;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace MyProduce.MyProduce
{
    /// <summary>
    /// 妊检
    /// </summary>
    public class BreedingpatternAppService : ApplicationService, IBreedingpatternAppService
    {
      private readonly IRepository<Pregnancytestresult, int> _pregnancytestrsult;
        private readonly IRepository<Prenatal,int > _prenatal;
        public BreedingpatternAppService(IRepository<Pregnancytestresult, int> pregnancytestrsult, IRepository<Prenatal, int> prenatal)
        {
            _pregnancytestrsult = pregnancytestrsult;
            _prenatal = prenatal;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="appearanceDTO"></param>
        /// <returns></returns>

        public  async Task<EarResult<PrentalDto>> PrentalAdds(PrentalDto appearanceDTO)
        {
            Prenatal prenatal = new Prenatal();
           prenatal.PregnancyDate=appearanceDTO.PregnancyDate;
            prenatal.Variety = appearanceDTO.Variety;
            prenatal.Transfertobuilding = appearanceDTO.Transfertobuilding;
            prenatal.Variety = appearanceDTO.Variety;
            prenatal.Cottage = appearanceDTO.Cottage;
            prenatal.Field = appearanceDTO.Field;
            prenatal.Overbit = appearanceDTO.Overbit;
            prenatal.Surrenderdate = appearanceDTO.Surrenderdate;
            prenatal.RoundType = appearanceDTO.RoundType;
            prenatal.Breedingpattern = appearanceDTO.Breedingpattern;
            prenatal.Speciesestrustype = appearanceDTO.Speciesestrustype;
            prenatal.BreedfemaleRAMS = appearanceDTO.BreedfemaleRAMS;
            prenatal.Dateofbreeding = appearanceDTO.Dateofbreeding;
            prenatal.principal = appearanceDTO.principal;
            prenatal.Pregnancydays = appearanceDTO.Pregnancydays;
            prenatal.Testmode = appearanceDTO.Testmode;
            prenatal.Pregnancytestresult = appearanceDTO.Pregnancytestresult;
            prenatal.Paritynumber = appearanceDTO.Paritynumber;
            prenatal.Drivefield=appearanceDTO.Drivefield;
            var result = await _prenatal.InsertAsync(prenatal);
            var res = ObjectMapper.Map<Prenatal, PrentalDto>(result);
            if (res != null)
            {
                return new EarResult<PrentalDto>() { Code = 200, Message = "添加成功" };
            }
            else
            {
                return new EarResult<PrentalDto>() { Code = 500, Message = "添加失败" };
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Aid"></param>
        /// <returns></returns>

        public async Task Deletepren(int Aid)
        {
            await _prenatal.DeleteAsync(Aid);
        }

        /// <summary>
        /// 显示妊检结果
        /// </summary>
        /// <returns></returns>

        public  async Task<List<PregnancytestresultDto>> GetBreedingPatternsAsync()
        {
            var result = await _pregnancytestrsult.GetListAsync();
            return ObjectMapper.Map<List<Pregnancytestresult>, List<PregnancytestresultDto>>(result);
        }
        /// <summary>
        /// 显示妊检列表
        /// </summary>
        /// <returns></returns>
        public  async Task<List<PrentalDto>> GetPrenatalsAsync(string? Mother, string? Pregnancyresult, string? Fatherear)
        {
           var result =  await _prenatal.GetListAsync();
            if (!string.IsNullOrEmpty(Mother))
            {
                result = result.Where(x => x.Overbit.Contains(Mother)).ToList();
            }
            if (!string.IsNullOrEmpty(Pregnancyresult))
            {
                result = result.Where(x => x.Pregnancytestresult.Contains(Pregnancyresult)).ToList();
            }
            if (!string.IsNullOrEmpty(Fatherear))
            {
                result = result.Where(x => x.Paritynumber.Contains(Fatherear)).ToList();
            }
            return ObjectMapper.Map<List<Prenatal>, List<PrentalDto>>(result);

        }

        public List<object> GetPregnancyEnumShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<PregnancyEnum>())
            {
                list.Add(new { value = (int)item, name = item.ToString() });
            }
            return list.ToList();
        }
        /// <summary>
        /// 同胎数枚举
        /// </summary>
        /// <returns></returns>
        public List<object> GetParitynumberEnumShow()
        {
            List<object> list = new List<object>();
            foreach (var item in Enum.GetValues<Paritynumber>())
            {
                list.Add(new { value = (int)item, name = item.ToString() });
            }
            return list.ToList();
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="eid"></param>
        /// <returns></returns>
        public async Task<PrentalDto> GetCampFindaa(int eid)
        {
            var result = await _prenatal.GetAsync(eid);
            return ObjectMapper.Map<Prenatal, PrentalDto>(result);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        /// <exception cref="UserFriendlyException"></exception>

        public  async Task<EarResult<PrentalDto>> UpdateAsync(PrentalDto dto)
        {
            var res = await _prenatal.FindAsync(dto.Id);
            if (res == null)
            {
                throw new UserFriendlyException("没有找到对应的用户");
            }
            ObjectMapper.Map(dto, res);
            var count = await _prenatal.UpdateAsync(res);
            var result = ObjectMapper.Map<Prenatal, PrentalDto>(count);

            if (result != null)
            {
                return new EarResult<PrentalDto>() { Code = 200, Message = "修改成功！" };
            }
            else
            {
                return new EarResult<PrentalDto>() { Code = 500, Message = "修改失败！" };
            }
        }

    
    }
}
